package servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Database.StaffAppointmentDB;
import Model.Approval;
import Model.StaffAppointment;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Servlet implementation class AppAllPrint
 */
@WebServlet("/AppAllPrint")
public class AppAllPrint extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String appointmentID = null;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AppAllPrint() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	// To be implemented!
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		AuditLogDB.insertLog("print appointment", (String) request.getSession()
				.getAttribute("name"));

		int appointmentID = Integer.parseInt(request
				.getParameter("allAppointment"));
		// ArrayList<Approval> app = StaffAppointmentDB
		// .getAllApprovedAppointments(appointmentID);

		String appointmentName = StaffAppointment
				.getAppointmentName(appointmentID);
		response.setContentType("application/pdf");
		Document myDocument = new Document(PageSize.A4, 36, 36, 30, 70);
		String imageUrl = "http://" + request.getServerName() + ":"
				+ request.getServerPort()
				+ request.getServletContext().getContextPath()
				+ "/images/The_Nanyang_Polytechnic_Logo.jpg";
		try {

			PdfWriter.getInstance(myDocument, response.getOutputStream());

			myDocument.setMarginMirroring(true);

			myDocument.open();
			//	Adds NYP Logo to PDF
			myDocument.add(addLogo(imageUrl));
			Font fontbold = new Font();
			fontbold.setStyle(Font.BOLD);
			Paragraph paragraph = new Paragraph("APPOINTMENT", fontbold);
			paragraph.setAlignment(Element.ALIGN_LEFT);
			myDocument.add(paragraph); // Heading
			myDocument
					.add(new Paragraph(
							"______________________________________________________________________________")); // Divider
			Chunk apptName = new Chunk(appointmentName);
			apptName.setUnderline(1f, -2f);
			apptName.setFont(fontbold);
			//	Prints the appointment name selected to PDF
			myDocument.add(apptName);
			//	XXX Start of pdf output code specific to 'Personal Mentor'
			if (appointmentName.equalsIgnoreCase("Personal Mentor")) {
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("Refer to the attached list."));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("Roles and Responsibilities",
						fontbold));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph(StaffAppointment
						.getResponsibilitiesForAppointment(appointmentName)));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("     ")); // spacing
				myDocument.add(new Paragraph("     ")); // spacing

				myDocument.add(new Paragraph(
						"Approved by:___________________________________"));

				myDocument
						.add(new Paragraph(
								"            "
										+ "          Director, School Of Information Technology"));

				myDocument.add(new Paragraph("     ")); // spacing

				myDocument.add(new Paragraph("     ")); // spacing
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				myDocument.add(new Paragraph("Date : " + (sdf.format(date))));

				myDocument.add(new Paragraph("     ")); // spacing
				myDocument.add(new Paragraph("     ")); // spacing

				Font f = new Font();
				f.setColor(BaseColor.BLUE);
				f.setStyle(Font.ITALIC);
				myDocument
						.add(new Paragraph(
								"This is an electronic copy. No signature is required.",
								f));
				myDocument.newPage();
				myDocument.add(addLogo(imageUrl));
				Paragraph paragraph1 = new Paragraph("APPOINTMENT", fontbold);
				paragraph.setAlignment(Element.ALIGN_LEFT);
				myDocument.add(paragraph1); // Heading
				myDocument
						.add(new Paragraph(
								"______________________________________________________________________________")); // Divider
				myDocument.add(apptName);
				GregorianCalendar gc = new GregorianCalendar();
				int year = gc.get(Calendar.YEAR) - 5;
				int counter = 1;
				int yearTest = gc.get(Calendar.YEAR);
				System.out.println(year);
				PdfPTable rootTable = new PdfPTable(2);
				rootTable.setWidthPercentage(100f);
				for(;yearTest != gc.get(Calendar.YEAR) - 6 ; yearTest--)
				{
					try{
						PdfPCell pemCell = new PdfPCell();
						pemCell.setPadding(0);
				//		pemCell.setBorder(Rectangle.NO_BORDER);
						pemCell.addElement(generateFormForPEM(appointmentName,yearTest));
						rootTable.addCell(pemCell);
					}catch(NullPointerException e)
					{
						e.printStackTrace();
					}
				}
				myDocument.add(rootTable);
			}
			//	XXX End of pdf output code for Personal Mentor
			//===========================================================================//
			//	XXX Start of pdf output code for Semester Team Leader
			else if (appointmentName.equalsIgnoreCase("Semester Team Leader")) {
				ArrayList<StaffAppointment> staffAptList = StaffAppointment
						.getStaffsForAppointment(appointmentName);
				myDocument
						.add(generateFormForSTL(appointmentName, staffAptList));
				myDocument.add(new Paragraph("Roles and Responsibilities",
						fontbold));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph(StaffAppointment
						.getResponsibilitiesForAppointment(appointmentName)));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("     ")); // spacing
				myDocument.add(new Paragraph("     ")); // spacing

				myDocument.add(new Paragraph(
						"Approved by:___________________________________"));

				myDocument
						.add(new Paragraph(
								"            "
										+ "          Director, School Of Information Technology"));

				myDocument.add(new Paragraph("     ")); // spacing

				myDocument.add(new Paragraph("     ")); // spacing
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				myDocument.add(new Paragraph("Date : " + (sdf.format(date))));

				myDocument.add(new Paragraph("     ")); // spacing
				myDocument.add(new Paragraph("     ")); // spacing

				Font f = new Font();
				f.setColor(BaseColor.BLUE);
				f.setStyle(Font.ITALIC);
				myDocument
						.add(new Paragraph(
								"This is an electronic copy. No signature is required.",
								f));
			} else if (appointmentName.equalsIgnoreCase("Course Coordinator")) {
				PdfPTable rootTable = new PdfPTable(2);
				rootTable.setWidthPercentage(100);
				int counter = 0;
				
				myDocument.add(generateFormForCC(appointmentName));
				myDocument.add(new Paragraph("Roles and Responsibilities",
						fontbold));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph(StaffAppointment
						.getResponsibilitiesForAppointment(appointmentName)));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph(
						"Approved by:___________________________________"));

				myDocument
						.add(new Paragraph(
								"            "
										+ "          Director, School Of Information Technology"));

				myDocument.add(new Paragraph("     ")); // spacing

				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				myDocument.add(new Paragraph("Date : " + (sdf.format(date))));
				Font f = new Font();
				f.setColor(BaseColor.BLUE);
				f.setStyle(Font.ITALIC);
				myDocument
						.add(new Paragraph(
								"This is an electronic copy. No signature is required.",
								f));
			} else {
				ArrayList<StaffAppointment> staffApptList = new ArrayList<StaffAppointment>();
				StaffAppointment sa = new StaffAppointment();
				staffApptList = StaffAppointment
						.getStaffsForAppointment(appointmentName);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("Refer to the attached list."));
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(Chunk.NEWLINE);
				myDocument.add(new Paragraph("Roles and Responsibilities",
						fontbold));
				myDocument.add(Chunk.NEWLINE);
				for (StaffAppointment staffapt : staffApptList) {
					myDocument
							.add(new Paragraph(staffapt.getResponsibilities()));
					break;
				}

				myDocument.add(new Paragraph(
						"Approved by:___________________________________"));

				myDocument
						.add(new Paragraph(
								"            "
										+ "          Director, School Of Information Technology"));

				myDocument.add(new Paragraph("     ")); // spacing

				myDocument.add(new Paragraph("     ")); // spacing
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				myDocument.add(new Paragraph("Date : " + (sdf.format(date))));

				myDocument.add(new Paragraph("     ")); // spacing
				myDocument.add(new Paragraph("     ")); // spacing

				Font f = new Font();
				f.setColor(BaseColor.BLUE);
				f.setStyle(Font.ITALIC);
				myDocument
						.add(new Paragraph(
								"This is an electronic copy. No signature is required.",
								f));
			}
			// line
			// for (int i = 0; i < 1; i++) {
			// Chunk App_name = new Chunk(app.get(i).getAppointment()); //
			// Appointment
			// System.out.println(App_name);
			//
			// App_name.setUnderline(1f, -2f);
			// myDocument.add(App_name);
			// }
			//
			// //
			// // for (int j = 0; j < commRole.size(); j++) {
			// // out.print(commRole.get(j).getName() + "<br/><br/>"+"");
			// //
			// // for (int j = 0; j < commRole.size(); j++) {
			// //
			// // String[] arr = commRole.get(j).getStaffName().split(", ");
			// // for (int k = 0; k < arr.length; k++) {
			// // if (k == arr.length - 1) {
			// // out.print(Database.StaffDB.getStaffNamebyId(arr[k])+
			// // "<br/> ");
			// // } else {
			// // out.print(Database.StaffDB.getStaffNamebyId(arr[k])
			// // + "<br/> ");
			// // }
			// myDocument.add(new Paragraph("     "));
			//
			// if (app.get(i).getAppointment().equals("Semester Team Leader")) {
			//
			// // //********************************************** Table
			// // for semester team
			// //
			// leader*************************************************************************//
			// PdfPTable table1 = createTable1();
			// myDocument.add(table1);
			// //
			//
			// }
			// // /////////////////////////////////////////////// course C
			// // ////////////////////////
			//
			// else if (app.get(i).getAppointment()
			// .equals("Course Coordinator")) {
			//
			// PdfPTable table2 = createTable2();
			// myDocument.add(table2);
			// PdfPTable table1 = createTable3();
			// myDocument.add(table1);
			// PdfPTable table4 = createTable4();
			// myDocument.add(table4);
			// PdfPTable table5 = createTable5();
			// myDocument.add(table5);
			// PdfPTable table6 = createTable6();
			// myDocument.add(table6);
			// PdfPTable table7 = createTable7();
			// myDocument.add(table7);
			// PdfPTable table8 = createTable8();
			// myDocument.add(table8);
			//
			// }
			//
			// else if (app.get(i).getAppointmentID().equals("102")) {
			// myDocument.add(new Paragraph(
			// "  Please Refer to tables below   ")); // spacing
			// }
			//
			// // for (int j = 0; j < app.size(); j++)
			// // {
			// // String[] arr = app.get(j).getName().split(",");
			// //
			// // for (int k = 0; k < arr.length; k++) {
			// // if(k==arr.length -1){
			// // Chunk dip_code = new Chunk(app.get(j).getDiploma());
			// // myDocument.add(new Paragraph(dip_code));// Appointment
			// //
			// // }else{ Chunk dip_code = new Chunk(app.get(j).getDiploma());
			// // myDocument.add(new Paragraph( dip_code));
			// // }
			// // }
			// //
			//
			// // for(int s=0;s<app.size(); s++){
			// //
			// // Chunk staff_Name = new Chunk(app.get(s).getName());//
			// // Appointment
			// // Chunk dip_code = new Chunk(app.get(s).getDiploma());
			// // myDocument.add(new Paragraph(dip_code+"   " + staff_Name));
			// // // List of names
			// //
			// // }
			//
			// // PdfPTable table = createTable1();
			// // myDocument.add(table);
			// //
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			//
			// Chunk Roles = new Chunk("Roles & Responsibilities", fontbold);
			// Roles.setUnderline(1f, -2f); // Roles & Responsibilities
			// // heading
			// myDocument.add(Roles);
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			// for (int r = 0; r < 1; r++) {
			//
			// Chunk res = new Chunk(app.get(r).getResponsibility());
			// myDocument.add(new Paragraph(res)); // list
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			// myDocument.add(new Paragraph("     ")); // spacing
			//
			// myDocument.add(new Paragraph(
			// "Approved by:___________________________________"));
			//
			// myDocument
			// .add(new Paragraph(
			// "            "
			// + "          Director, School Of Information Technology"));
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			// Date date = new Date();
			// SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			// myDocument
			// .add(new Paragraph("Date : " + (sdf.format(date))));
			//
			// myDocument.add(new Paragraph("     ")); // spacing
			// myDocument.add(new Paragraph("     ")); // spacing
			//
			// Font f = new Font();
			// f.setColor(BaseColor.BLUE);
			// f.setStyle(Font.ITALIC);
			// myDocument
			// .add(new Paragraph(
			// "This is an electronic copy. No signature is required.",
			// f));
			//
			// // Personal Mentor Tables
			//
			// if (app.get(i).getAppointmentID().equals("102")) {
			//
			// PdfPTable table9 = createTable9();
			// myDocument.add(table9);
			// PdfPTable table10 = createTable10();
			// myDocument.add(table10);
			// PdfPTable table11 = createTable11();
			// myDocument.add(table11);
			// // PdfPTable table12 = createTable12();
			// // myDocument.add(table12);
			// // PdfPTable table13 = createTable13();
			// // myDocument.add(table13);
			//
			// }
			//
			// // of
			// } // Roles
			// // &
			// // Responsibilities
			// }
			//
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		}

		myDocument.close();
	}

	private PdfPTable createTable1() throws DocumentException {
		PdfPTable table1 = new PdfPTable(4);
		table1.setWidthPercentage(419 / 5.23f);
		table1.setWidths(new int[] { 3, 3, 3, 3 });

		table1.addCell("Course ");
		table1.addCell("Stage 1");
		table1.addCell("Stage 2");
		table1.addCell("Stage 3");

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);

		for (int w = 0; w < app1.size(); w++) {

			if (app1.get(w).getYearNo().equals("1")) {

				table1.addCell(app1.get(w).getDiploma());

				table1.addCell(app1.get(w).getName());

			}

			else if (app1.get(w).getYearNo().equals("2")) {

				table1.addCell(app1.get(w).getName());

			}

			else if (app1.get(w).getYearNo().equals("3")) {

				table1.addCell(app1.get(w).getName());

			}

		}

		return table1;

	}

	private PdfPTable createTable2() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);

		PdfPCell cell;
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.getDefaultCell().setVerticalAlignment(Element.ALIGN_LEFT);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		table1.setSpacingAfter(-80);// /////

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {

				int n = Integer.parseInt(app1.get(a).getYear());
				ArrayList list = new ArrayList();
				String s = app1.get(a).getYear();
				list.add(s);
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;

				if (app1.get(a).getDiploma().equals("DIT")
						&& app1.get(a).getAppointmentID().equals("101")) {
					if ((nn <= 4)) {
						String name = app1.get(a).getName();
						String dip = app1.get(a).getDiploma();
						String year = app1.get(a).getYear().substring(2, 4);

						table1.addCell(dip + year + "     " + name);

					}
				}
			}
		}
		return table1;

	}

	private PdfPTable createTable3() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table1.setWidthPercentage(50);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {

				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;

				if (app1.get(a).getDiploma().equals("DBI")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);

				}
			}

		}

		return table1;

	}

	private PdfPTable createTable4() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		table1.setSpacingBefore(40);
		table1.setSpacingAfter(-80);

		for (int a = 0; a < app1.size(); a++) {

			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {
				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;
				if (app1.get(a).getDiploma().equals("DEI")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);
				}

			}

		}

		return table1;

	}

	private PdfPTable createTable5() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table1.setWidthPercentage(50);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {
				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;
				if (app1.get(a).getDiploma().equals("DBT")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);
				}

			}
		}

		return table1;

	}

	private PdfPTable createTable6() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		table1.setSpacingBefore(40);
		table1.setSpacingAfter(-80);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {

				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;
				if (app1.get(a).getDiploma().equals("DFI")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);
				}
			}

		}

		return table1;

	}

	private PdfPTable createTable7() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table1.setWidthPercentage(50);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {

				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;
				if (app1.get(a).getDiploma().equals("DIS")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);

				}
			}

		}

		return table1;

	}

	private PdfPTable createTable8() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setTotalWidth(new float[] { 144 });

		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		table1.setSpacingBefore(40);
		table1.setSpacingAfter(40);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getYear() != null
					&& !app1.get(a).getYear().trim().isEmpty()) {

				String s = app1.get(a).getYear();
				int years = Integer.parseInt(s);

				Calendar cal = Calendar.getInstance();
				int currentYear = cal.get(cal.YEAR);

				int nn = currentYear - years;
				if (app1.get(a).getDiploma().equals("DBA")
						&& app1.get(a).getAppointmentID().equals("101")) {
					// if((nn <= 4)){
					String name = app1.get(a).getName();
					String dip = app1.get(a).getDiploma();
					String year = app1.get(a).getYear().substring(2, 4);

					table1.addCell(dip + year + "     " + name);

				}
			}

		}

		return table1;

		// ******************************** Personal
		// Mentor************************

	}

	private PdfPTable createTable9() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.setSpacingBefore(40);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		// table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);

		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		table1.setSpacingAfter(-136);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(cal.YEAR);
		int pemyear = currentYear - 0;

		table1.addCell("Year  " + pemyear);

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getAppointmentID().equals("102")) {
				if (app1.get(a).getYear() != null
						&& !app1.get(a).getYear().trim().isEmpty()) {

					String s = app1.get(a).getYear();
					int years = Integer.parseInt(s);
					int nn = currentYear - years;

					if (nn == 0) {
						if (app1.get(a).getDiploma().equals("DIT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DEI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DFI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DIS")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBA")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						}
					}
				}

			}
		}

		return table1;

	}

	private PdfPTable createTable10() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.setSpacingBefore(40);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);

		table1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table1.setWidthPercentage(50);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(cal.YEAR);
		int pemyear = currentYear - 1;
		table1.addCell("Year  " + pemyear);

		//

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getAppointmentID().equals("102")) {
				if (app1.get(a).getYear() != null
						&& !app1.get(a).getYear().trim().isEmpty()) {

					String s = app1.get(a).getYear();
					int years = Integer.parseInt(s);
					int nn = currentYear - years;

					if (nn == 1) {
						if (app1.get(a).getDiploma().equals("DIT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DEI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DFI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DIS")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBA")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						}
					}
				}

			}
		}

		return table1;

	}

	private PdfPTable createTable11() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.setSpacingBefore(100);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);

		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(cal.YEAR);
		int pemyear = currentYear - 2;
		table1.addCell("Year  " + pemyear);

		//

		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getAppointmentID().equals("102")) {
				if (app1.get(a).getYear() != null
						&& !app1.get(a).getYear().trim().isEmpty()) {

					String s = app1.get(a).getYear();
					int years = Integer.parseInt(s);
					int nn = currentYear - years;

					if (nn == 2) {
						if (app1.get(a).getDiploma().equals("DIT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DEI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBT")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DFI")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DIS")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						} else if (app1.get(a).getDiploma().equals("DBA")
								&& app1.get(a).getAppointmentID().equals("102")) {

							String name = app1.get(a).getName();
							String pemClass = app1.get(a).getSaclass();
							table1.addCell(pemClass + "     " + name);

						}
					}
				}

			}
		}

		return table1;

	}

	private PdfPTable createTable12() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.setSpacingBefore(40);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);

		table1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table1.setWidthPercentage(50);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(cal.YEAR);
		int pemyear = currentYear - 1;
		table1.addCell("Year  " + pemyear);

		//
		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getAppointmentID().equals("102")) {

				//
				// String s = app1.get(a).getSaclass();
				// int years = Integer.parseInt(s);
				// int nn = currentYear - years;
				//
				// if (nn == 1) {
				if (app1.get(a).getDiploma().equals("DIT")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DEI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBT")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DFI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DIS")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBA")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				}
			}
		}

		// }

		return table1;

	}

	private PdfPTable createTable13() throws DocumentException {
		PdfPTable table1 = new PdfPTable(1);
		table1.setWidthPercentage(130 / 5.23f);
		table1.setWidths(new int[] { 3 });
		table1.setSpacingBefore(40);
		ArrayList<Approval> app1 = StaffAppointmentDB
				.getAllApprovedAppointments(appointmentID);
		// table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);

		table1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table1.setWidthPercentage(50);
		// table1.setSpacingAfter();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

		Calendar cal = Calendar.getInstance();
		int currentYear = cal.get(cal.YEAR);
		int pemyear = currentYear - 4;

		table1.addCell("Year  " + pemyear);
		for (int a = 0; a < app1.size(); a++) {
			if (app1.get(a).getAppointmentID().equals("102")) {

				//
				// String s = app1.get(a).getSaclass();
				// int years = Integer.parseInt(s);
				// int nn = currentYear - years;
				//
				// if (nn == 1) {
				if (app1.get(a).getDiploma().equals("DIT")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DEI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBT")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DFI")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DIS")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				} else if (app1.get(a).getDiploma().equals("DBA")
						&& app1.get(a).getAppointmentID().equals("102")) {

					String name = app1.get(a).getName();
					String pemClass = app1.get(a).getSaclass();
					table1.addCell(pemClass + "     " + name);

				}
			}
		}

		// }

		return table1;

	}

	/**
	 * Generates PDF appointment form for Semestral Team Leaders
	 * 
	 * @param staffAptList
	 * @return
	 */
	private PdfPTable generateFormForSTL(String appointmentName,
			ArrayList<StaffAppointment> staffAptList) {
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		PdfPTable semesterTeamLeaderTable = new PdfPTable(new float[] { 1, 2,
				2, 2 });
		semesterTeamLeaderTable.setSpacingAfter(20.0f);
		semesterTeamLeaderTable.addCell(new Phrase("Course"));
		for (int i = 1; i <= 3; i++) {
			semesterTeamLeaderTable.addCell(new Phrase("Stage " + i));
		}
		StaffAppointment prevStaff = new StaffAppointment();
		;
		for (int i = 0; i < staffAptList.size(); i++) {
			StaffAppointment staffAppointment = staffAptList.get(i);
			if (!prevStaff.getDiploma().equalsIgnoreCase(
					staffAppointment.getDiploma())) {
				PdfPCell courseCell = new PdfPCell(new Phrase("D"
						+ staffAppointment.getDiploma().toUpperCase()));
				semesterTeamLeaderTable.addCell(courseCell);
			}
			switch (Integer.parseInt(staffAppointment.getYearNo())) {
			case 1: {
				PdfPCell nameCell = new PdfPCell(new Phrase(
						staffAppointment.getName(), baseFont));
				semesterTeamLeaderTable.addCell(nameCell);
				prevStaff = staffAppointment;
				break;
			}
			case 2: {
				if (!staffAppointment.getDiploma().equalsIgnoreCase(
						prevStaff.getDiploma())) {
					semesterTeamLeaderTable.addCell(new Phrase(""));

				} else {
					if (Integer.parseInt(prevStaff.getYearNo()) != 1) {
						semesterTeamLeaderTable.addCell(new Phrase(""));
					}
				}
				semesterTeamLeaderTable.addCell(new Phrase(staffAppointment
						.getName(), baseFont));

				prevStaff = staffAppointment;
				break;
			}
			case 3: {
				System.out.println(staffAppointment.getName() + "\n Year :"
						+ staffAppointment.getYearNo() + "\nDiploma :"
						+ staffAppointment.getDiploma());
				if (!staffAppointment.getDiploma().equalsIgnoreCase(
						prevStaff.getDiploma())) {
					semesterTeamLeaderTable.addCell(new Phrase(""));
					semesterTeamLeaderTable.addCell(new Phrase(""));
				} else {
					if (Integer.parseInt(prevStaff.getYearNo()) == 1) {
						semesterTeamLeaderTable.addCell(new Phrase(""));
					}
				}
				semesterTeamLeaderTable.addCell(new Phrase(staffAppointment
						.getName(), baseFont));
				prevStaff = staffAppointment;
				break;
			}
			}
			if (i < staffAptList.size() - 1) {
				if (!staffAppointment.getDiploma().equalsIgnoreCase(
						staffAptList.get(i + 1).getDiploma())) {
					semesterTeamLeaderTable.completeRow();
				}
			}
		}
		return semesterTeamLeaderTable;
	}

	/**
	 * Generates PDF appointment form for Personal Mentors
	 * 
	 * @return PdfPTable
	 */
	private PdfPTable generateFormForPEM(String appointmentName) {
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		System.out.println("In GENERATE FORM FOR PEM");
		PdfPTable personalMentorListTable = new PdfPTable(1);
		// personalMentorListTable.setWidthPercentage(50);
		// personalMentorListTable.setHorizontalAlignment(Element.ALIGN_LEFT);
		// personalMentorListTable.setSpacingBefore(20f);
		personalMentorListTable.setSpacingAfter(30f);
		StaffAppointment sa = new StaffAppointment();
		ArrayList<StaffAppointment> staffAptList = new ArrayList<StaffAppointment>();
		staffAptList = StaffAppointment
				.getStaffsForAppointment(appointmentName);
		int year = 0;
		String sAptClass = "";
		for (int i = 0; i < staffAptList.size(); i++) {
			sa = staffAptList.get(i);
			if (year != Integer.parseInt(sa.getYear())) {
				PdfPCell yearCell = new PdfPCell(new Phrase("Year "
						+ sa.getYear()));
				// yearCell.setColspan(1);
				personalMentorListTable.addCell(yearCell);
				year = Integer.parseInt(sa.getYear());
			}
			// Adds an empty cell if the first two characters of the class is
			// different
			if (!sa.getSaclass().regionMatches(true, 0, sAptClass, 0, 2)
					&& !sAptClass.equals("")) {
				PdfPCell emptyCell = new PdfPCell(new Phrase("     "));
				emptyCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
				personalMentorListTable.addCell(emptyCell);
			}
			PdfPCell pemCell = new PdfPCell(new Phrase(sa.getSaclass()
					+ "       " + sa.getName().toUpperCase(), baseFont));
			if (i == staffAptList.size() - 1) {
				pemCell.setBorder(Rectangle.LEFT | Rectangle.BOTTOM
						| Rectangle.RIGHT);
			} else if (i < staffAptList.size() - 1
					&& i != staffAptList.size() - 1) {
				// checks if year in year counter is different from the year in
				// the next element in the arraylist
				if (year != Integer.parseInt(staffAptList.get(i + 1).getYear())) {
					pemCell.setBorder(Rectangle.LEFT | Rectangle.BOTTOM
							| Rectangle.RIGHT);
				} else {
					pemCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
				}

			}
			personalMentorListTable.addCell(pemCell);
			sAptClass = sa.getSaclass();
		}
		return personalMentorListTable;
	}
	
	/**
	 * Generates PDF appointment table for Personal Mentors by year
	 * 
	 * @param appointmentName
	 *            - The Name of the appointment assigned
	 * @param year
	 *            - the year of the appointment
	 * @return PdfPTable
	 * @see AppAllPrint#generateFormForPEM(String)
	 */
	private PdfPTable generateFormForPEM(String appointmentName, int year) {
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		try {
		PdfPTable personalMentorListTable = new PdfPTable(1);
//		personalMentorListTable.setTotalWidth(150.0f);
		personalMentorListTable.setWidthPercentage(100);
	//	personalMentorListTable.setLockedWidth(true);
		StaffAppointment sa = new StaffAppointment();
		ArrayList<StaffAppointment> staffAptList = new ArrayList<StaffAppointment>();
		staffAptList = StaffAppointment.getStaffsForAppointmentByYear(
				appointmentName, year);
		String sAptClass = "";	
			PdfPCell yearCell = new PdfPCell(new Phrase("Year " + year));
			personalMentorListTable.addCell(yearCell);
			for (int i = 0; i < staffAptList.size(); i++) {
				sa = staffAptList.get(i);
				// Adds an empty cell if the first two characters of the class
				// is
				// different
				if (!sa.getSaclass().regionMatches(true, 0, sAptClass, 0, 2)
						&& !sAptClass.equals("")) {
					PdfPCell emptyCell = new PdfPCell(new Phrase("     "));
					emptyCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
					personalMentorListTable.addCell(emptyCell);
				}
				PdfPCell pemCell = new PdfPCell(new Phrase(sa.getSaclass()
						+ "       " + sa.getName().toUpperCase(), baseFont));
				if (i == staffAptList.size() - 1) {
					pemCell.setBorder(Rectangle.LEFT
							| Rectangle.RIGHT);
				} else if (i < staffAptList.size() - 1
						&& i != staffAptList.size() - 1) {
					pemCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
				}
				personalMentorListTable.addCell(pemCell);
				sAptClass = sa.getSaclass();
			}
			return personalMentorListTable;
		} catch (NullPointerException nullPtrEx) {
			nullPtrEx.printStackTrace();
			return null;
		}
	}

	/**
	 * Generates PDF appointment table for Personal Mentors by year
	 * 
	 * @param appointmentName
	 *            - The Name of the appointment assigned
	 * @param year
	 *            - the year of the appointment
	 * @param oddEven
	 * 			  - to determine the alignment off the table
	 * @return PdfPTable
	 * @see AppAllPrint#generateFormForPEM(String)
	 */
	private PdfPTable generateFormForPEM(String appointmentName, int year,
			int oddEven) {
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		try {
		PdfPTable personalMentorListTable = new PdfPTable(1);
//		personalMentorListTable.setTotalWidth(150.0f);
		personalMentorListTable.setWidthPercentage(50.0f);
	//	personalMentorListTable.setLockedWidth(true);
		StaffAppointment sa = new StaffAppointment();
		ArrayList<StaffAppointment> staffAptList = new ArrayList<StaffAppointment>();
		staffAptList = StaffAppointment.getStaffsForAppointmentByYear(
				appointmentName, year);
		String sAptClass = "";
		if(oddEven % 2 != 0)
		{
			personalMentorListTable.setHorizontalAlignment(Rectangle.ALIGN_LEFT);
		}else
		{
			personalMentorListTable.setHorizontalAlignment(Rectangle.ALIGN_RIGHT);
		}
	
			PdfPCell yearCell = new PdfPCell(new Phrase("Year " + year));
			// yearCell.setColspan(1);
			personalMentorListTable.addCell(yearCell);
			for (int i = 0; i < staffAptList.size(); i++) {
				sa = staffAptList.get(i);
				// Adds an empty cell if the first two characters of the class
				// is
				// different
				if (!sa.getSaclass().regionMatches(true, 0, sAptClass, 0, 2)
						&& !sAptClass.equals("")) {
					PdfPCell emptyCell = new PdfPCell(new Phrase("     "));
					emptyCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
					personalMentorListTable.addCell(emptyCell);
				}
				PdfPCell pemCell = new PdfPCell(new Phrase(sa.getSaclass()
						+ "       " + sa.getName().toUpperCase(), baseFont));
				if (i == staffAptList.size() - 1) {
					pemCell.setBorder(Rectangle.LEFT | Rectangle.BOTTOM
							| Rectangle.RIGHT);
				} else if (i < staffAptList.size() - 1
						&& i != staffAptList.size() - 1) {
					pemCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
				}
				personalMentorListTable.addCell(pemCell);
				sAptClass = sa.getSaclass();
			}
			return personalMentorListTable;
		} catch (NullPointerException nullPtrEx) {
			nullPtrEx.printStackTrace();
			return null;
		}
	}

	/**
	 * Generates PDF appointment form for Course Coordinators
	 * 
	 * @return
	 */
	private PdfPTable generateFormForCC(String appointmentName) {
		PdfPTable courseCoordinatorList = new PdfPTable(2);
		courseCoordinatorList.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
		courseCoordinatorList.setWidthPercentage(100);
		courseCoordinatorList.setSpacingAfter(30f);
		PdfPCell emptyCell = new PdfPCell(new Phrase(""));
		emptyCell.setBorder(Rectangle.NO_BORDER);
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		StaffAppointment prevStaff = new StaffAppointment();
		PdfPCell cCordCell = new PdfPCell();
		cCordCell.setBorder(Rectangle.NO_BORDER);
		Phrase testPhrase = new Phrase();
		testPhrase.setFont(baseFont);
		int counter = 1;
		for (StaffAppointment sa : StaffAppointment
				.getStaffsForAppointment(appointmentName)) {
//			cCordCell = new PdfPCell(new Phrase("D"
//					+ sa.getDiploma().toUpperCase() + sa.getYear().substring(2)
//					+ "    " + sa.getName().toUpperCase(), baseFont));
		
			if(!prevStaff.getDiploma().equals(sa.getDiploma()) && prevStaff.getDiploma() != "")
			{
				testPhrase.add("\n");
				counter++;
				if(counter == 4)
				{
					System.out.println("In if counter == 4 :");
					cCordCell.setPhrase(testPhrase);
					courseCoordinatorList.addCell(cCordCell);
				}if(counter == 5)
				{
					testPhrase = new Phrase();
					testPhrase.setFont(baseFont);
				}
			}
				testPhrase.add("D" + sa.getDiploma().toUpperCase() + sa.getYear().substring(2) + "    " 
						+ sa.getName().toUpperCase() + "\n");
			prevStaff = sa;
		}
		cCordCell.setPhrase(testPhrase);
		courseCoordinatorList.addCell(cCordCell);
		return courseCoordinatorList;
	}

	/**
	 * Queries the imagePath provided and try to create an instance of the image
	 * found and return the image for further processing
	 * 
	 * @param imageUrl
	 * @return image
	 */
	protected Image addLogo(String imageUrl) {
		try {
			System.out.println("Image path: " + imageUrl);
			Image image = Image.getInstance(new URL(imageUrl));
			image.scalePercent(10);
			return image;
		} catch (Exception ex) {
			System.out.println(ex);
		}
		return null;
	}
	
	protected Chunk addSignature()
	{
		return new Chunk();
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
}